import javax.sql.rowset.serial.SerialStruct;
import java.util.Arrays;
import java.util.Locale;
import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2022-08-10
 * Time: 21:59
 */
class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name=" + name +
                ", age=" + age +
                '}';
    }
}

public class TestDemo {
    public static void main(String[] args) {
        // 检测字符串是否是以某个子串开头或结尾
        String str = "hello world";
        boolean ret1 = str.startsWith("hell");
        System.out.println(ret1);
        System.out.println(str.endsWith("rld"));
    }

    public static void main9(String[] args) {
        // 检测字符串中是否包含某一个子串
        String str1 = "hello  hello        ";
        boolean ret1 = str1.contains("hello");
        System.out.println(ret1);

        // 去掉字符串左右两边的空格
        String ret2 = str1.trim();
        System.out.println(ret2 + "ppppp");
    }

    public static void main8(String[] args) {
        // 字符串截取
        String str = "helloworld";
        String ret1 = str.substring(1);
        String ret2 = str.substring(1, 4);
        System.out.println(ret1);
        System.out.println(ret2);
    }

    public static void main7(String[] args) {
        // 字符串替换
        String str1 = "ababcabcdabcdef";
        String ret1 = str1.replace('a', 'R');
        System.out.println(ret1);
        String ret2 = str1.replace("ab", "PK");
        System.out.println(ret2);
    }

    public static void main6(String[] args) {
        // 将字符串中的每一个元素放进字符数组
        String str1 = "Hello";
        char[] chars = str1.toCharArray();
        System.out.println(Arrays.toString(chars));
    }

    public static void main5(String[] args) {
        // 将字符串中的小写字母转换为大写字母
        String str1 = "abc";
        String ret1 = str1.toUpperCase();
        System.out.println(ret1);
        // 如果不是小写字母，其它都不变
        String str2 = "Hello World 你好";
        String ret2 = str2.toUpperCase();
        System.out.println(ret2);

        // 同理，将字符串中的大写字母与转换为小写字母
        String str3 = "HELLO WORLD";
        String ret3 = str3.toLowerCase();
        System.out.println(ret3);
    }

    public static void main4(String[] args) {
        // 将字符串转为其它类型
        int val1 = Integer.parseInt("123");
        // 或
        int val2 = Integer.valueOf("123");
        System.out.println(val1);
        System.out.println(val2);

        double val3 = Double.parseDouble("12.10");
        System.out.println(val3);
    }

    public static void main3(String[] args) {
        // 将其它类型转为字符串  valueOf()
        int a = 123;
        String str1 = String.valueOf(a);
        System.out.println(str1);
        boolean b = true;
        String str2 = String.valueOf(b);
        System.out.println(str2);
        String str3 = String.valueOf(new Student("张三", 16));
        System.out.println(str3);
    }

    public static void main2(String[] args) {
        // 如何比较两个字符串
        String str1 = "hello";
        String str2 = "world";
        String str3 = "hello";
        String str4 = "Hello";
        // 错误的比较
        System.out.println(str1 == str2);
        // 正确的比较
        System.out.println(str1.equals(str2));// false
        System.out.println(str1.equals(str3));// true

        // 忽略字符串的大小写来比较
        str1.equalsIgnoreCase(str4);// true

    }

    public static void main1(String[] args) {
        // 创建字符串
        // 两种方法
        // 1、
        String str1 = "hello";
        System.out.println(str1);
        // 2、
        String str2 = new String("hello");
        System.out.println(str2);

        // 将字符数组传入字符串
        char[] chars = {'a', 'b', 'c'};
        String str3 = new String(chars);
        System.out.println(str3);
    }
}
